﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model;
using Model.BusinessModel.OutPut;
using Model.BusinessModel;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Repository.Sys
{
    /// <summary>
    /// 系统菜单仓储接口实现
    /// </summary>
    public class SysMenuRepository : Repository<SysMenu>, ISysMenuRepository
    {
        /// <summary>
        /// 获取OverallAuth系统树形菜单
        /// </summary>
        /// <returns></returns>
        public List<SysMenu> GetMenuTreeList()
        {
            string sql = " select Id,CorporationKey,SystemKey,Path,MenuIcon,Name,Pid,IsOpen,Sort,CreateTime,CreateUser,Component,RequireAuth,Redirect from Sys_Menu where  CorporationKey=@CorporationKey and SystemKey=@SystemKey";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysMenu>(sql, new { CorporationKey = Const.XC_CorporationKey, SystemKey = Const.OverallAuth_SystemKey }).ToList();
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        public List<SysMenuOutPut> GetAllMenuTreeList()
        {
            string sql = @" SELECT sm.*,sc.CorporationName,ss.SystemName,su.UserName FROM Sys_Menu as sm
                            inner join Sys_Corporation as sc on sm.CorporationKey = sc.CorporationKey
                            inner join Sys_System as ss on ss.SystemKey = sm.SystemKey 
                            inner join Sys_User as su on sm.CreateUser = su.UserId";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysMenuOutPut>(sql).ToList();
        }

        /// <summary>
        /// 获取公司下系统的菜单
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns></returns>
        public List<TreeOutPut> GetCorporationSystemMenuList(string corporationKey, string systemKey)
        {
            StringBuilder sql = new(@" select * from (
                                select Id as [Key],case WHEN Pid='0' THEN  CONVERT(VARCHAR(36), SystemKey) else CONVERT(VARCHAR(36), Pid) end AS [PKey]  , Name as  label,SystemKey,CorporationKey,MenuIcon as icon,4 as nodeType
                                from  Sys_Menu 
                                UNION 
                                SELECT CONVERT(VARCHAR(36), SystemKey)  as [Key], '' as  [PKey] , SystemName as  [Value],SystemKey,CorporationKey,'HomeFilled' icon,2 as nodeType
                                 from Sys_System) as t");

            //StringBuilder sql = new(@" 
            //                    select Id as [Key],Pid AS [PKey]  , Name as  label,SystemKey,CorporationKey,MenuIcon as icon,4 as nodeType
            //                    from  Sys_Menu 
            //                    ");

            var qp = new QueryParameter();
            if (!string.IsNullOrWhiteSpace(corporationKey))
            {
                qp.listWhere.Append(" and CorporationKey = @CorporationKey ");
                qp.dynamicParameter.Add("@CorporationKey", corporationKey);
            }
            if (!string.IsNullOrWhiteSpace(systemKey))
            {
                qp.listWhere.Append(" and SystemKey = @SystemKey ");
                qp.dynamicParameter.Add("@SystemKey", systemKey);
            }

            if (!string.IsNullOrEmpty(qp.listWhere.ToString()))
            {
                sql.Append(" WHERE 1=1 " + qp.listWhere.ToString());
            }
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<TreeOutPut>(sql.ToString(), qp.dynamicParameter).ToList();
        }

        /// <summary>
        /// 获取公司、系统、菜单数据 
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns></returns>
        public List<TreeOutPut> GetCorporationSystemMenuData(string corporationKey, string systemKey)
        {
            StringBuilder sql = new(@" select * from (
                                select Id as [Key],case WHEN Pid='0' THEN  CONVERT(VARCHAR(36), SystemKey) else CONVERT(VARCHAR(36), Pid) end AS [PKey]  , Name as  label,SystemKey,CorporationKey,MenuIcon as icon,4 as nodeType
                                from  Sys_Menu 
                                UNION 
                                SELECT CONVERT(VARCHAR(36), SystemKey)  as [Key], CorporationKey as  [PKey] , SystemName as  [Value],SystemKey,CorporationKey,'Histogram' icon ,2 as nodeType
                                 from Sys_System
                                 UNION 
                                SELECT CONVERT(VARCHAR(36), CorporationKey)  as [Key], '' as  [PKey] , CorporationName as  [Value],'' SystemKey,CorporationKey,'OfficeBuilding' icon ,1 as nodeType
                                 from Sys_Corporation 
                                ) as t");

            var qp = new QueryParameter();
            if (!string.IsNullOrWhiteSpace(corporationKey) && Const.XC_CorporationKey != corporationKey)
            {
                qp.listWhere.Append(" and t.CorporationKey = @CorporationKey ");
                qp.dynamicParameter.Add("@CorporationKey", corporationKey);
            }
            if (!string.IsNullOrWhiteSpace(systemKey))
            {
                qp.listWhere.Append(" and t.SystemKey = @SystemKey ");
                qp.dynamicParameter.Add("@SystemKey", systemKey);
            }

            if (!string.IsNullOrEmpty(qp.listWhere.ToString()))
            {
                sql.Append(" WHERE 1=1 " + qp.listWhere.ToString());
            }
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<TreeOutPut>(sql.ToString(), qp.dynamicParameter).ToList();
        }

        /// <summary>
        /// 根据菜单id获取菜单
        /// </summary>
        /// <param name="menuId">菜单id集合</param>
        /// <returns></returns>
        public List<SysMenu> GetMenusByMenuIdList(List<string> menuId)
        {
            string sql = " select * from  Sys_Menu where Id in @Key ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysMenu>(sql, new { Key = menuId }).ToList();
        }

        /// <summary>
        /// 根据公司和系统获取数据
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns></returns>
        public List<SysMenu> GetMenuByCorporationKeyOrSystemKey(string corporationKey, string systemKey)
        {
            string sql = @" select * from Sys_Menu where CorporationKey=@CorporationKey and SystemKey=@SystemKey ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysMenu>(sql, new { CorporationKey = corporationKey, SystemKey = systemKey }).ToList();
        }
    }
}
